package org.neusoft.auth.service.impl;

import org.neusoft.auth.entity.Sysfun;
import org.neusoft.auth.mapper.SysfunMapper;
import org.neusoft.auth.service.SysfunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysfunServiceImpl  implements SysfunService {
    @Autowired
    SysfunMapper sysfunMapper;
    @Override
    public Sysfun getSysfunTreeByLogid(String logid) {
        List<Sysfun> lss = sysfunMapper.getByLogId(logid);
        Sysfun sysfun =null;
        for ( Sysfun sf: lss) {
            if (sf.getPid() == 1 ) {
                sysfun = sf;
                break;
            }
        }
        buildTree( lss,sysfun );
        return sysfun;
    }

    private void buildTree(List<Sysfun> lss, Sysfun sysfun) {
        if (sysfun== null)
            return;
        for ( Sysfun sf : lss) {
            if (sf.getPid() == sysfun.getId()){
                sysfun.getChildren().add(sf);
            }
        }
        if (sysfun.getChildren().size() == 0 )
            return;
        for ( Sysfun sf  : sysfun.getChildren()) {
            buildTree(lss, sf);
        }
    }
}
